import {createStore,applyMiddleware} from 'redux'
import Thunk from 'redux-thunk'

//1.定义action创建函数

function collect_action(item) {
    return {
        type: 'COLLECT',
        item
    }
}
function cancel_collect_action(id) {
    return {
        type: 'CANCEL_COLLECT',
        id
    }
}
function add_address_action(item) {
    return {
        type: 'ADD_ADDRESS',
        item
    }
}
function edit_address_action(item) {
    return {
        type: 'EDIT_ADDRESS',
        item
    }
}
function delete_address_action(id) {
    return {
        type: 'DELETE_ADDRESS',
        id
    }
}
function set_default_address_action(item) {
    return {
        type: 'SET_DEFAULT_ADDRESS',
        item
    }
}

var data = {
    last_id:0,//保存 最后一次新增的地址id
    addressList:[],//保存新增的地址
    collectList: [], //保存收藏的店铺
    defaultAddress:{}//保存选中的默认地址
}

//2.定义reducer处理函数
function reducer(state = data, action)
{
    
    //深拷贝
    state = JSON.parse(JSON.stringify(state));

    switch (action.type)
    {
        case 'COLLECT':// 收藏 动作
            state.collectList.push(action.item);
            console.log(state.collectList);
            
            return state;
            
        case 'CANCEL_COLLECT':// 取消收藏 动作
            //查找 对应id的店铺对象
            var findIndex = state.collectList.findIndex((item) => { return item.id === action.id });
            if (findIndex !== -1) {
                state.collectList.splice(findIndex, 1);
            }
            return state;

        case 'ADD_ADDRESS':// 新增地址 动作
            
            //action.item.id = state.addressList.length + 1; //这样写,地址对象的id可能重复
            action.item.id = state.last_id + 1;
            state.addressList.push(action.item);
            state.last_id = action.item.id;// 更新 最后一次新增地址的id
            console.log('last_id', state.last_id,state.addressList);
            
            return state;
            
        case 'EDIT_ADDRESS':// 更新地址 动作
            //查找 对应id的店铺对象
            var findIndex = state.addressList.findIndex((item) => { return item.id === action.item.id });
            if (findIndex !== -1) {
                state.addressList.splice(findIndex, 1, action.item);
            }
            return state;

        case 'DELETE_ADDRESS':// 删除地址 动作
            //查找 对应id的店铺对象
            var findIndex = state.addressList.findIndex((item) => { return item.id === action.id });
            if (findIndex !== -1) {
                state.addressList.splice(findIndex, 1);
                //删除地址的同时,如果该地址是默认地址, 也要删除默认地址
                if (action.id === state.defaultAddress.id)
                    state.defaultAddress = {};
            }
            return state;
            
        case 'SET_DEFAULT_ADDRESS':// 设置默认地址 动作
            state.defaultAddress = action.item;
            return state;

        default:
            return state;
    }
}

//3.创建store
var store = createStore(reducer,applyMiddleware( Thunk ));

//导出
export {
    store,
    collect_action,
    cancel_collect_action,
    add_address_action,
    edit_address_action,
    delete_address_action,
    set_default_address_action
}
